//
//  LastFiveDayVC.m
//  AnimationRecommend
//
//  Created by ljm on 2017/2/22.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "LastFiveDayVC.h"
#import "VTMagic.h"

#import "SJAvatarBrowser.h"

#import "TitleCell.h"
#import "ImageViewCell.h"
#import "ContentsCell.h"
#import "MyButtonCell.h"
#import "JudgeTVC.h"
#import "ARLoginViewController.h"

@interface LastFiveDayVC()<cellBtnClickedDelegate>

@property (nonatomic, strong) NSArray *infoList;

@end

@implementation LastFiveDayVC

{
    NSArray *nowData ;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //生成随机数
    NSInteger index = 15 +  (arc4random() % 4);
    nowData = [NSArray arrayWithArray:self.infoList[index]];
    
    self.tableView.scrollsToTop = NO;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    //    self.view.backgroundColor = RGBCOLOR(239, 239, 239);
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //    self.tableView.rowHeight = self.view.frame.size.height - AAdaption(64);
    //    [self fetchNewsData];
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    VTPRINT_METHOD
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    self.tableView.scrollsToTop = YES;
    VTPRINT_METHOD
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.tableView.scrollsToTop = NO;
    VTPRINT_METHOD
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    VTPRINT_METHOD
}

#pragma mark -- 自定义cell的代理方法
- (void)cellBtnClick:(UIButton *)btn {
    
    BOOL loginStatus =  [[NSUserDefaults standardUserDefaults] boolForKey:isLoginKey];
    if (loginStatus == NO) {
        
        __weak typeof(self) weakSelf = self;
        [[CLLScottAlertView alloc] showEffAlertViewWithTitle:@"提示" message:@"您还未登录,是否登录?" WithSureBlock:^(ScottAlertAction *action) {
            
            [weakSelf presentViewController:[[ARLoginViewController alloc] init] animated:YES completion:nil];
            
        } WithCancelBlock:^(ScottAlertAction *action){
            
        }WithVc:self];
        
        return;
    }
    
    if (btn.tag == 101) {
        //点赞
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        hud.label.text = @"✔️";
        hud.detailsLabel.text = @"已点赞";
        hud.backgroundColor = RGBACOLOR(0.8, 0.8, 0.8, 0.8f);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            hud.hidden = YES;
        });
        
        
    }else {
        //评价
        
        JudgeTVC *jvc = [[JudgeTVC alloc]init];
        jvc.title = @"评价详情";
        
        CATransition *transition = [CATransition animation];
        
        transition.duration = 0.6f;
        
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
        
        transition.type = kCATransitionPush;
        
        transition.subtype = kCATransitionFromTop;
        
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
        
        [self.navigationController pushViewController:jvc animated:YES];
    }
}


#pragma mark -- 每个tableview有几个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return AAdaption(100);
    }else if (indexPath.section == 1) {
        return AAdaption(kBaseWidth - 160);
    }else if (indexPath.section == 2){
        ContentsCell *cell = [ContentsCell loadCellWithTableView:tableView];
        return  CGRectGetMaxY(cell.detailSourceLabel.frame) - CGRectGetMinY(cell.detailTitleLabel.frame) + AAdaption(30);
    }else {
        return AAdaption(80);
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        TitleCell *cell = [TitleCell loadCellWithTableView:tableView];
        cell.titleLabel.text = nowData[1];// @"《火影忍者》";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    if (indexPath.section == 1) {
        ImageViewCell *cell = [ImageViewCell loadCellWithTableView:tableView];
        NSString *urlStr = [NSString stringWithFormat:@"%@",nowData[0]];
        [cell.imageview sd_setImageWithURL:[NSURL URLWithString:urlStr]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    if (indexPath.section == 2) {
        ContentsCell *cell = [ContentsCell loadCellWithTableView:tableView];
        cell.contentLabel.text = nowData[2];// @"真正重要的东西，就算失去自己珍贵的生命，也要用双手保护到底；2. 你以为这个世界上只有你一个人与众不同么？ ";
        cell.detailSourceLabel.text = nowData[3];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    if (indexPath.section == 3) {
        MyButtonCell *cell = [MyButtonCell loadCellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        return cell;
    }
    
    return nil;
    
}

#pragma mark -- 允许menu 菜单，长按出现选择菜单
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark -- 支持每个action
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    //默认复制(copy)、剪切(cut)、粘贴(paste)
    //    return YES;
    if (action == @selector(copy:) && indexPath.section == 2) {
        return YES;
    }
    return NO;
}

#pragma mark -- 选中文字的action （tableview自带）
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    
    if (action == @selector(copy:))
    {
        ContentsCell *cell = (ContentsCell *)[tableView cellForRowAtIndexPath:indexPath];
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
        [pasteBoard setString:cell.contentLabel.text];
    }
}

#pragma mark -- 点击图片选中事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"选中了蔡亮亮臭傻逼");
    if (indexPath.section == 1) {
        NSString *urlStr = [NSString stringWithFormat:@"%@",nowData[0]];
        UIImageView *imageV = [[UIImageView alloc] init];
        [imageV sd_setImageWithURL:[NSURL URLWithString:urlStr]];
        [SJAvatarBrowser showImage:imageV];
    }
}

#pragma mark - VTMagicReuseProtocol
- (void)vtm_prepareForReuse
{
    // reset content offset
    NSLog(@"clear old data if needed:%@", self);
    [self.tableView setContentOffset:CGPointZero];
}

#pragma mark -- getter
- (NSArray *)infoList
{
    if (!_infoList ) {
        _infoList = [NSArray array];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"CartoonSource" ofType:@"plist"];
        _infoList = [NSArray arrayWithContentsOfFile:path];
    }
    return _infoList;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
